package com.djl.mvc.controller;

import com.djl.crowd.entity.Role;
import com.djl.crowd.util.ResultEntity;
import com.djl.service.RoleService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author le
 * @Date 2021/11/20 15:21
 * @Version 1.0
 */
@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/role/remove.json")
    public ResultEntity removeRole(@RequestBody List<Integer> roleIds) {
        System.out.println(roleIds.toString());
        roleService.removeRole(roleIds);
        return ResultEntity.successWithoutData();
    }


    @PostMapping("/role/update.json")
    public ResultEntity updateRole(Role role) {
        int rows = roleService.updateRole(role);
        return rows > 0 ? ResultEntity.successWithoutData() : ResultEntity.failed(null);
    }

    @PostMapping("/role/save.json")
    public ResultEntity saveRole(Role role) {
        int rows = roleService.saveRole(role);
        return rows > 0 ? ResultEntity.successWithoutData() : ResultEntity.failed(null);
    }

    @PostMapping("/role/get/page/info.json")
    public ResultEntity getPageInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                    @RequestParam(value = "keyword", defaultValue = "") String keyword
    ) {

        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);

        //封装到ResultEntity对象中返回（如果上面的操作跑异常，交给异常映射机制处理）
        log.info(ResultEntity.successWithData(pageInfo).toString());
        return ResultEntity.successWithData(pageInfo);
    }

}
